package com.zb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.zb.entity.ZjCode03;
import com.zb.entity.ZjCode04;
import com.zb.entity.ZjCodeInfo04;
import com.zb.entity.ZjCodeLog04;
import com.zb.mapper.ZjCode03Mapper;
import com.zb.mapper.ZjCode04Mapper;
import com.zb.service.*;
import com.zb.utils.TransactionUtil;
import com.zb.vo.CodeRelateoinInfoVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionStatus;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * @Author WEN
 * @Date 2023/8/29 0029
 **/
@Service
public class ZjCode04ServiceImpl extends ServiceImpl<ZjCode04Mapper, ZjCode04> implements ZjCode04Service {
    @Resource
    private ZjCode04Mapper code04Mapper;
    @Resource
    private PlatformTransactionManager transactionManager;
    @Resource
    private ZjCodeInfo04Service codeInfo04Service;
    @Resource
    private ZjCodeLog04Service codeLog04Service;

    @Override
    public void save( CountDownLatch countDownLatch, List<ZjCode04> codeList, List<ZjCodeInfo04> codeInfos, List<ZjCodeLog04> codeLogs) {
        if (null == codeList || codeList.isEmpty()) {
            countDownLatch.countDown();
            return;
        }
        TransactionStatus status = transactionManager.getTransaction(TransactionUtil.transactionDefinition());
        try {
            this.saveBatch(codeList);
            codeInfo04Service.saveBatch(codeInfos);
            codeLog04Service.saveBatch(codeLogs);
            transactionManager.commit(status);
        }catch (Exception e) {
            transactionManager.rollback(status);
        }finally {
            countDownLatch.countDown();
        }
    }

    public void save(List<ZjCode04> codeList, List<ZjCodeInfo04> codeInfos, List<ZjCodeLog04> codeLogs) {
        if (null == codeList || codeList.isEmpty()) {
            return;
        }
        this.saveBatch(codeList);
        codeInfo04Service.saveBatch(codeInfos);
        codeLog04Service.saveBatch(codeLogs);
    }

    // 根据编码ID激活编码
    @Override
    public void updateBatchActivCodeByIds(List<ZjCode04> code04s, List<ZjCodeLog04> codeLog04s) {
        if (null == code04s || code04s.isEmpty()) {
            return;
        }
        this.updateBatchById(code04s);
        codeLog04Service.saveBatch(codeLog04s);
    }

    // 根据编码ID删除编码
    @Override
    public void deleteBatchByIds(List<Long> codeIds) {
        if (null == codeIds || codeIds.isEmpty()) {
            return;
        }
        this.removeByIds(codeIds);
        codeInfo04Service.removeByIds(codeIds);
        codeLog04Service.deleteBatchByCodeIds(codeIds);
    }

    // 根据编码批次ID查询检查是否编码有关联信息
    @Override
    public List<CodeRelateoinInfoVo> checkCodeRelationInfoForDeleteByBatchIds(List<Long> codeBatchIds) {
        return code04Mapper.checkCodeRelationInfoForDeleteByBatchIds(codeBatchIds);
    }

    // 根据批次ID获取编码ID
    @Override
    public List<Long> getIdByBatchId(Long batchId) {
        QueryWrapper<ZjCode04> wrapper = new QueryWrapper<>();
        wrapper.select("id");
        wrapper.eq("batch_id", batchId);
        return this.list(wrapper).stream().map(ZjCode04::getId).toList();
    }

    // 根据编码ID查询检查是否编码有关联信息
    @Override
    public List<CodeRelateoinInfoVo> checkCodeRelationInfoByIds(List<Long> ids) {
        return code04Mapper.checkCodeRelationInfoByIds(ids);
    }
}
